package extraWork;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.URL;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import extraWork.AboutFrame;

@SuppressWarnings("serial")
public class TextEdit extends JFrame implements Runnable, ActionListener, CommonConstants {
	File current = null;

	JFileChooser fc = null;

	JTextArea ta = null;

	JFrame about = null;

	URL url = null;

	public TextEdit() throws Exception {
		url = this.getClass().getResource("img/icon.JPG");
		// Typing Area
		ta = new JTextArea(100, 50);
		ta.setText("");
		// For saving and opening
		fc = new JFileChooser();
		// MENU START
		// File Menu
		JMenuItem exit = new JMenuItem("Exit     		");
		JMenuItem save = new JMenuItem("Save    		");
		JMenuItem open = new JMenuItem("Open     		");
		JMenuItem _new = new JMenuItem("New      		");
		JMenuItem saveAs = new JMenuItem("Save As...");
		_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		_new.setActionCommand(NEW);
		_new.addActionListener(this);
		save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		save.setActionCommand(SAVE);
		save.addActionListener(this);
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		open.setActionCommand(OPEN);
		open.addActionListener(this);
		exit.setActionCommand(EXIT);
		exit.addActionListener(this);
		saveAs.setActionCommand(SAVE_AS);
		saveAs.addActionListener(this);
		JMenu file = new JMenu("File");
		file.add(_new);
		file.add(open);
		file.add(save);
		file.add(saveAs);
		file.addSeparator();
		file.add(exit);
		// View Menu
		JRadioButtonMenuItem wlaf = new JRadioButtonMenuItem("Windows", false);
		wlaf.setActionCommand(LAF + WLAF);
		wlaf.addActionListener(this);
		JRadioButtonMenuItem glaf = new JRadioButtonMenuItem("GTK+", false);
		glaf.setActionCommand(LAF + GLAF);
		glaf.addActionListener(this);
		JRadioButtonMenuItem mlaf = new JRadioButtonMenuItem("Motif", false);
		mlaf.setActionCommand(LAF + MLAF);
		mlaf.addActionListener(this);
		JRadioButtonMenuItem jlaf = new JRadioButtonMenuItem("Metal", true);
		jlaf.setActionCommand(LAF + JLAF);
		jlaf.addActionListener(this);
		ButtonGroup viewGroup = new ButtonGroup();
		viewGroup.add(wlaf);
		viewGroup.add(glaf);
		viewGroup.add(mlaf);
		viewGroup.add(jlaf);
		JMenu viewMenu = new JMenu("View");
		viewMenu.add(wlaf);
		viewMenu.add(glaf);
		viewMenu.add(mlaf);
		viewMenu.add(jlaf);
		// Help Menu
		JMenuItem about = new JMenuItem("About TextEdit	");
		about.setIcon(new ImageIcon(url));
		about.setActionCommand(ABOUT);
		about.addActionListener(this);
		JMenu help = new JMenu("Help");
		help.add(about);
		// Menu bar
		JMenuBar mb = new JMenuBar();
		mb.add(file);
		mb.add(viewMenu);
		mb.add(help);
		// MENU END
		// Panel to display everything properly
		JPanel p1 = new JPanel();
		p1.add(ta);
		// Required for scroll bars
		JViewport view = new JViewport();
		view.add(p1);
		// Scroll bar stuff
		JScrollPane scroll = new JScrollPane(view);
		JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
		scroll.setVerticalScrollBar(scrollBar);
		scroll.setName("Text1");
		// For testing tabs
		JPanel nextTab = new JPanel();
		nextTab.setName("empty");
		// Tabbed panels
		JTabbedPane tabs = new JTabbedPane();
		tabs.add(scroll, 0);
		tabs.add(nextTab, 1);
		this.getContentPane().add(tabs);
		this.setJMenuBar(mb);
	}

	public void actionPerformed(ActionEvent pE) {
		if (pE.getActionCommand().equals(EXIT)) {
			this.dispose();
			System.exit(0);
		} else if (pE.getActionCommand().equals(SAVE)) {
			if (current == null) {
				fc.showSaveDialog(this);
				current = fc.getSelectedFile();
			}
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter(current));
				out.flush();
				out.write(ta.getText());
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (pE.getActionCommand().equals(OPEN)) {
			ta.setText("");
			fc.showOpenDialog(this);
			current = fc.getSelectedFile();
			int ch;
			try {
				BufferedReader in = new BufferedReader(new FileReader(current));
				while (in.ready()) {
					ch = in.read();
					ta.append(Character.toString((char) ch));
				}
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (pE.getActionCommand().equals(ABOUT)) {
			String msg = " TextEdit (R) was created by Darcy Coleman April 2008 \n      (C) Darcy Coleman 2008 All Rights Reserved.";
			AboutFrame about = new AboutFrame("", msg, 250, 200, this.getX(), this.getY(), this.getWidth(), this.getWidth());
			about.run();
		} else if (pE.getActionCommand().equals(NEW)) {
			// possible save dialog and maybe use tabs later
			ta.setText("");
		} else if (pE.getActionCommand().equals(SAVE_AS)) {
			fc.showSaveDialog(this);
			fc.setCurrentDirectory(current);
			current = fc.getSelectedFile();
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter(current));
				out.flush();
				out.write(ta.getText());
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (pE.getActionCommand().startsWith(LAF)) {
			try {
				UIManager.setLookAndFeel(pE.getActionCommand().substring(3));
				SwingUtilities.updateComponentTreeUI(this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void run() {
		// Set the icon file
		try {
			ImageIcon icon = new ImageIcon(url);
			this.setIconImage(icon.getImage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Add everything to the main frame
		this.setTitle("TextEdit");
		this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		this.setSize(400, 300);
		this.setLocation(200, 250);
		this.setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		new TextEdit().run();
	}
}